//
//  PublicHomeClientPackProgressMaskView.m
//  HSBCHybrid
//
//  Created by gltc on 6/14/13.
//
//

#ifndef RGBACOLOR
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
#endif

#import <QuartzCore/QuartzCore.h>
#import "PublicHomeClientPackProgressMaskView.h"
#import "UILabel+VariableHeight.h"

// max/min height of content
const CGFloat kMaxHeightOfTextProgressMaskView = 70.0;
const CGFloat kMinHeightOfTextProgressMaskView = 44.0;
const CGFloat kySpaceOfTextProgressMaskView = 8.0;  // total space of both sides. 4x2

@interface PublicHomeClientPackProgressMaskView ()

- (void)setupBorder;
- (void)adjustHeightOfContent;

@end

@implementation PublicHomeClientPackProgressMaskView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        
    }
    return self;
}

-(void)initialize{
    [self setupBorder];
    [self setupBar];
}

- (void)setupBorder
{
    // set up loading view border
    self.layer.borderWidth = 1.0f;
    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //self.layer.cornerRadius = 6;
    self.layer.masksToBounds = YES;
}

- (void)setupBar
{
    self.processBar.backgroundColor = [UIColor clearColor];
    [self.processBar setOuterColor: [UIColor clearColor]];
    [self.processBar setInnerColor: [UIColor grayColor]];
    [self.processBar setEmptyColor: RGBACOLOR(221, 221, 221, 1)];
    [self.processBar setProgress:0.0f];
}

- (void)setMessage:(NSString*)tip
{
    // content
    self.commentLabel.text = tip;

    // adjust height
    [self adjustHeightOfContent];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [_processBar release];
    [_processLabel release];
    [_indicatorView release];
    [_commentLabel release];
    
    [super dealloc];
}

#pragma mark - property access
- (void)setProgress:(CGFloat)newProgress tip:(NSString*)sTip
{
    // verify new progress.
    if (newProgress > 1.0)
    {
        progress = 1.0;
    }
    else if(newProgress < 0.0)
    {
        progress = 0.0;
    }
    else
    {
        progress = newProgress;
    }
    
    // update progress bar
    self.processBar.progress = progress;
    
    // update progress label
    NSString *sCompleted = @"complete";
    NSString *percentString = [[NSString alloc] initWithFormat:@"%.0f%@%@", progress * 100, @"\% ", sCompleted];
    self.processLabel.text = percentString;
    [percentString release];
   self.center = self.superview.center;
    if(sTip && ![@"" isEqualToString:sTip]){
        self.commentLabel.text = sTip;
        [self adjustHeightOfContent];
    }
}

- (void)adjustHeightOfContent
{
    // calculate height
    CGFloat realHeight = [self.commentLabel calculateHeightWithMaxHeight:kMaxHeightOfTextProgressMaskView];
    
    // add total space of both sides
    realHeight += kySpaceOfTextProgressMaskView;
    
    // limit height
    if (realHeight < kMinHeightOfTextProgressMaskView) {
        realHeight = kMinHeightOfTextProgressMaskView;
    }
    
    // adjust height
    CGRect newFrame = self.commentLabel.frame;
    newFrame.size.height = realHeight;
    
    self.commentLabel.frame = newFrame;
}

@end
